package com.ctshk.rpc.finance.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.finance.dto.onaccount.CurrencyQuotaDTO;
import com.ctshk.rpc.finance.dto.onaccount.QueryQuotaDTO;
import com.ctshk.rpc.finance.entity.FinanceOnAccountCurrencyQuota;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 财务挂账币种额度 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-30
 */
public interface FinanceOnAccountCurrencyQuotaMapper extends BaseMapper<FinanceOnAccountCurrencyQuota> {

    /**
     * 获取挂账币种统计
     * @param id
     * @return
     */
    @Select("SELECT currency_id,currency AS currency_name,total_amount,used_amount,write_off_amount,(total_amount - used_amount) AS remainAmount\n" +
            "FROM t_finance_on_account_currency_quota\n" +
            "WHERE on_account_id = #{on_account_id} AND is_deleted = 0\n" +
            "GROUP BY currency_id")
    List<CurrencyQuotaDTO> queryListByCurrency(@Param("on_account_id") Long id);

    /**
     * 查询额度
     *
     * @param accountId
     * @param currencyId
     * @param businessType
     * @return
     */
    @Select("<script>"+"SELECT a.currency_id,a.currency,(a.total_amount - a.used_amount + a.write_off_amount) AS availableAmount \n" +
            "FROM t_finance_on_account_currency_quota a\n" +
            "LEFT JOIN t_finance_on_account b ON a.on_account_id = b.id\n" +
            "WHERE a.is_deleted = 0 AND b.is_deleted = 0 AND a.currency_id = #{currency_id}\n"+
            "<if test=\"account_type == 1\">AND b.account_id = #{account_id}</if> \n" +
            "<if test=\"account_type == 2\">AND b.dept_id = #{dept_id}</if> \n" +
            " limit 1"+"</script>")
    QueryQuotaDTO queryQuota(@Param("account_type") Integer accountType, @Param("dept_id") Long deptId, @Param("account_id") Long accountId, @Param("currency_id") Long currencyId, @Param("business_type") Integer businessType);
}
